package algorithmcase;

import java.util.ArrayList;
import java.util.Arrays;

public class Case3 {
    public static void main(String[] args) {
        int[] arr = {27,22,30,40,36,13,19,16,20,7,10,43,50,58};
        ArrayList<DisorderBlock> disorderBlockList = getDisorderBlockList(arr);
        for (DisorderBlock disorderBlock : disorderBlockList) {
            System.out.println(disorderBlock);
        }
    }
    private static ArrayList<DisorderBlock> getDisorderBlockList(int[] arr){
        ArrayList<DisorderBlock> disorderBlockList = new ArrayList<>();
        int startIndex = 0;
        for (int i = 2; i < arr.length;) {
            int[] subArr = Arrays.copyOfRange(arr,startIndex,i);
            System.out.println(subArr.length);
            System.out.println(subArr[0]+"+++"+subArr[1]);
            System.out.println("i--"+i);
            int[] compareArr = Arrays.copyOfRange(arr, i,arr.length);
            System.out.println(compareArr[0]);
            boolean flag = false;
            for (int k : compareArr) {
                if (isWithinRange(subArr, k)) {
                    flag = true;
                    break;
                }
            }
            if(flag){
                DisorderBlock disorderBlock = new DisorderBlock();
                disorderBlock.setStartIndex(startIndex);
                disorderBlock.setEndIndex(i);
                disorderBlock.setMax(getMax(subArr));
                disorderBlock.setMin(getMin(subArr));
                disorderBlockList.add(disorderBlock);
                System.out.println("截断节点++++"+i+"+++截断元素"+arr[i]);
                startIndex = i+1;
                i+=3;
            } else {
                i++;
            }
        }
        return disorderBlockList;
    }
    //获取数值中的最大值和最小值,并且判断一个数在不在这个区间
    private static boolean isWithinRange(int[] arr,int target){
        int max = arr[0];
        int min = arr[0];
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
            if (arr[i] < min) {
                min = arr[i];
            }
        }
        return target > min && target < max;
    }
    //写出找出数组中最小值和最大值的两个方法
    private static int getMax(int[] arr){
        int max = arr[0];
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
        }
        return max;
    }
    private static int getMin(int[] arr){
        int min = arr[0];
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] < min) {
                min = arr[i];
            }
        }
        return min;
    }

}
